package com.bjpowernode.gateway.mvc.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjpowernode.gateway.common.enums.TradeStatus;
import com.bjpowernode.gateway.common.util.NoUtil;
import com.bjpowernode.gateway.db.domain.TradeOrder;
import com.bjpowernode.gateway.db.domain.UserPayInfo;
import com.bjpowernode.gateway.db.mapper.TradeOrderMapper;
import com.bjpowernode.gateway.db.mapper.UserPayInfoMapper;
import com.bjpowernode.gateway.mvc.models.dto.InvokeApiResultDto;
import com.bjpowernode.gateway.mvc.models.dto.KuaiQianInvokeApiResultDto;
import com.bjpowernode.gateway.mvc.models.dto.KuaiQianPayInfoDto;
import com.bjpowernode.gateway.mvc.models.form.TradeConfirmForm;
import com.bjpowernode.gateway.mvc.service.ThirdApiService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service("KUAIQIAN")
public class KuaiQianThirdApiService implements ThirdApiService {
    @Resource
    UserPayInfoMapper userPayInfoMapper;

    @Resource
    TradeOrderMapper tradeOrderMapper;

    @Override
    public InvokeApiResultDto invokePayment(TradeConfirmForm confirmForm) {
        //查询支付 用户配置参数
        QueryWrapper<UserPayInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_no", confirmForm.getUserNo()).eq("pay_product_code",confirmForm.getPayProductCode()).eq("pay_way_code","KUAIQIAN");
        UserPayInfo userPayInfo = userPayInfoMapper.selectOne(queryWrapper);
        if(userPayInfo==null){
            throw new RuntimeException("商户无支付配置信息");
        }
        String payConfig = userPayInfo.getPayConfig();
        //创建第三方接口需要的参数
        KuaiQianPayInfoDto payInfoDto = new KuaiQianPayInfoDto();
        JSONObject payConfigMap = JSONUtil.parseObj(payConfig);
        payInfoDto.setMerchantAcctId(payConfigMap.getStr("merchId"));
        payInfoDto.setPublicFile(payConfigMap.getStr("publicFile"));
        payInfoDto.setPrivateFile(payConfigMap.getStr("privateFile"));
        payInfoDto.setQueryKey(payConfigMap.getStr("queryKey"));
        payInfoDto.setShopName(userPayInfo.getShopName());
        payInfoDto.setTrxNo("KQ"+ NoUtil.getTradeId());
        //快钱网银支付接口参数
        KuaiQianInvokeApiResultDto invokeApiResultDto = new KuaiQianInvokeApiResultDto(confirmForm,payInfoDto);
        //创建订单记录
        createTradeOrderRecord(confirmForm,userPayInfo,payInfoDto);
        return invokeApiResultDto;
    }

    private void createTradeOrderRecord(TradeConfirmForm confirmForm, UserPayInfo userPayInfo, KuaiQianPayInfoDto payInfoDto) {
        TradeOrder tradeOrder = new TradeOrder();
        tradeOrder.setId(NoUtil.getTradeId());
        tradeOrder.setUserNo(confirmForm.getUserNo());
        tradeOrder.setUserName(userPayInfo.getShopName());
        tradeOrder.setUserOrderNo(confirmForm.getOrderNo());
        tradeOrder.setTxNo(payInfoDto.getTrxNo());
        tradeOrder.setOrderAmount(new BigDecimal(confirmForm.getOrderMoney()));
        tradeOrder.setOrderTime(new DateTime());
        tradeOrder.setPayProductCode(confirmForm.getPayProductCode());
        tradeOrder.setPayWayCode("KUAIQIAN");
        tradeOrder.setPayWayName("快钱");
        tradeOrder.setPayTypeCode(confirmForm.getWayTypeCode().split(":")[1]);
        tradeOrder.setPayTypeName("--");
        tradeOrder.setPageurl(confirmForm.getPageUrl());
        tradeOrder.setNotifyurl(confirmForm.getNotifyUrl());
        tradeOrder.setRemark("创建支付订单");
        tradeOrder.setEditTime(new Date());
        tradeOrder.setStatus(TradeStatus.CREATE.name());
        tradeOrderMapper.insert(tradeOrder);
    }
}
